package org.logsin37.util.press.entity

import com.fasterxml.jackson.annotation.JsonInclude
import org.springframework.util.StopWatch

import java.time.LocalTime
import java.time.temporal.ChronoUnit

@JsonInclude(JsonInclude.Include.NON_NULL)
class TraceLog {

    String id
    String workerName
    boolean success
    LocalTime endTimeStamp
    long titleTime
    String errorMessage

    private StopWatch stopWatch

    static TraceLog init(String workerName) {
        def traceLog = new TraceLog()
        traceLog.id = UUID.randomUUID().toString()
        traceLog.workerName = workerName
        traceLog.stopWatch = new StopWatch(workerName)
        traceLog.stopWatch.start()
        traceLog
    }

    TraceLog success() {
        this.stopWatch.stop()
        this.success = true
        this.endTimeStamp = LocalTime.now().truncatedTo(ChronoUnit.SECONDS)
        this.titleTime = this.stopWatch.totalTimeMillis
        this
    }

    TraceLog error(String errorMessage) {
        this.stopWatch.stop()
        this.success = false
        this.endTimeStamp = LocalTime.now().truncatedTo(ChronoUnit.SECONDS)
        this.titleTime = this.stopWatch.totalTimeMillis
        this.errorMessage = errorMessage
        this
    }

}
